package com.scoresystem.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.scoresystem.common.CommonValue;
import com.scoresystem.dao.IRemarkDao;
import com.scoresystem.model.Remark;
import com.scoresystem.model.StuRemark;
import com.scoresystem.model.Subject;
import com.scoresystem.utils.ResultMapUtil;

public class RemarkDaoImpl extends BaseDaoImpl implements IRemarkDao {

	public RemarkDaoImpl(Connection conn) {
		super(conn);
		this.conn=conn;
	}

	@Override
	public boolean add(String stunum, String worknum, String remark) {
		String sql="insert into "+CommonValue.TAB_REMARK+" (stunum,worknum,remark)"
				+ " values ("+"'"+stunum+"'"+","+"'"+worknum+"'"+","+"'"+remark+"'"+")";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return false;

	}

	@Override
	public boolean modify(String stunum, String worknum, String remark) {
		String sql="update "+CommonValue.TAB_REMARK+" set remark="+"'"+remark+"'"
				+" where stunum="+"'"+stunum+"'"+" and worknum="+"'"+worknum+"'";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<StuRemark> listAll(String worknum) {
		String sql="select stunum,remark from "+CommonValue.TAB_REMARK+" where worknum="
				+"'"+worknum+"'";
		List<StuRemark> list=new ArrayList<StuRemark>();
		try {
			ResultSet rs=templateQuery(sql);
			while(rs.next()){
				Map<String,String> map=ResultMapUtil.getResultMap(rs);				
				StuRemark stu=new StuRemark();
				try {
					BeanUtils.populate(stu, map);
				} catch (Exception e) {
					e.printStackTrace();
				} 
				list.add(stu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}  
        
		return list;
	}

}
